/**
 * 
 */
package info.niwota.profile.web;

import info.niwota.commons.acl.AccessControl;
import info.niwota.commons.acl.AccessDeniedException;
import info.niwota.commons.web.ContentHelper;
import info.niwota.profile.Debug;
import info.niwota.profile.PeerListAdapter;
import info.niwota.profile.Preferences;
import info.niwota.profile.Profiles;
import info.niwota.webapp.acl.PermStore;

import java.io.File;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

/**
 * @author qiangli
 * 
 */
public class ProfileObject {

	public static final String NAME = "profile";

	private static final String TAG = "ProfileObject";

	public static final String APPS = "apps";
	public static final String PEERS = "peers";
	public static final String USER = "user";

	public static final String GET_PROFILE = "getProfile";
	public static final String LIST_PROFILE = "listProfile";

	private Context ctx;

	public ProfileObject(Context ctx) {
		this.ctx = ctx;
	}

	private static JSONObject getItem(File file) {
		//TODO performance: we do not need to whole object just name, email and photo
		try {
			JSONObject o = ContentHelper.readObject(file);
			return o;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new JSONObject();
	}
	
	public Object listProfile(JSONObject param, Uri uri) throws Exception {
		if (Debug.DEBUG) {
			Log.d(TAG, "getProfile param: " + param + " uri: " + uri);
		}
		
		String sort = uri.getQueryParameter("sort");
		JSONObject json = new JSONObject();

		JSONArray peers = new JSONArray();
		File[] profiles = Helper.listVisited(ctx);
		PeerListAdapter.sortProfiles(ctx, profiles,
				(sort != null && sort.startsWith("fav") ? PeerListAdapter.SORT_FAV : PeerListAdapter.SORT_RECENT));
		if (profiles != null) {
			for (int i = 0; i < profiles.length; i++) {
				peers.put(getItem(profiles[i]));
			}
		}

		json.put(PEERS, peers);

		return json;
	}

	public Object getProfile(JSONObject param, Uri uri) throws Exception {
		if (Debug.DEBUG) {
			Log.d(TAG, "getProfile param: " + param + " uri: " + uri);
		}
		String label = Label.LABEL_MY_PROFILE;
		String group = uri.getQueryParameter("group");
		String user = uri.getQueryParameter("user");

		int g = (TextUtils.isEmpty(group) ? AccessControl.ACCESS_PUBLIC
				: Integer.parseInt(group));

		//
		if (!canAccess(g, user, label)) {
			throw new AccessDeniedException("No permission: " + user);
		}

		String peer = uri.getQueryParameter("peer");
		String jid = uri.getQueryParameter("jid");

		if (Debug.DEBUG) {
			Log.d(TAG, "######## peer: " + peer + " jid: " + jid);
		}

		JSONObject jo = null;

		if (TextUtils.isEmpty(peer) || peer.equals(jid)) {
			jo = Preferences.getProfileJSONObject(ctx, true);
			jo.put("peer", ""); // local self
		} else {
			boolean nocache = Boolean.parseBoolean(uri
					.getQueryParameter("nocache"));
			jo = Helper.getPeerProfile(ctx, peer, nocache);
			jo.put("peer", peer);
		}
		long modified = jo.getLong(Preferences.PROFILE_MODIFIED);
		jo.put("modified", Profiles.formatModified(modified));

		return jo;
	}

	private boolean canAccess(int group, String user, String label) {
		String[] pa = PermStore.getPerm(ctx, label);
		String perm = pa[0];
		String emails = pa[1];

		return AccessControl.canAccess(group, user, label, perm, emails);
	}
}
